Learning Flex Basics (Part 4): Working with Data Models
Robert Crooks
Table of Contents
- Introduction
- Create the Application Object and Application Layout
- Add the Form Controls and the DataGrid Controls
- Add the New Employee Model, Employee, and Location Models
- Instantiate the DataController Class, and Create the Employee and DataController Classes
- Add the DataController Methods
- Code Listing for data.mxml
- Code Listings for DataController.as and Employee.as
- Code Listings for locations.xml and employee.xml
- Where to Go From Here
In this tutorial, you work with data models both for importing data and storing data entered by users in Macromedia Flex applications. You also learn more about manipulating data in applications by using data entered in a form to create a new object, and by implementing drag and drop between different data grids.
This tutorial is the fourth part in a four-part series written by Robert Crooks of the Macromedia Customer Training group:
- Learning Flex Basics (Part 1): Creating Your First Flex Application
- Learning Flex Basics (Part 2): Creating a Flex Calculator
- Learning Flex Basics (Part 3): Working with Containers
If you prefer, you can download the entire set of tutorials.
-
Learning
Flex Series (Parts 1-4) (144 KB)
If you like what you see and want to get more experience with Flex, Macromedia Customer Training offers Fast Track to Macromedia Flex, a two-day onsite training course to jump-start Flex development in your organization. Now let's get started!
As the final tutorial in this series, the aim is partly to familiarize you with additional elements of the Flex class library, and with some additional techniques such as implementing drag and drop interfaces. This tutorial also intends to make you more aware of how you can use Flex to separate out and loosen coupling between the view, data model, and controller portions of an application.
The controller methods know nothing about the details of the view, for instance.
The method that adds a new employee takes the data from a model, and so doesn't
need to know how you go about gathering the data to put there. Similarly,
the methods that handle the drag-and-drop operations work from methods and
properties of the event objects that Flex generates and the built-in DragManager
class, so they need not know where the data objects are dragged from and
dropped—in fact, all you need to do is add the dragEnabled="true" attribute
to the target data grids to enable dragging items from one to the other;
the controller methods will work without modification.
What You Will Learn
- How to use the Application tag.
- How to use a Panel container.
- How to use the HBox.
- How to use the VBox.
- How to use the Form container.
- How to use TextInput controls.
- How to use Button controls.
- How to use DataGrid controls.
- How to use the Model class for importing data or gathering user input.
- How to enable drag and drop for items in a DataGrid.
- How to create event handlers for drag and drop operations in ActionScript.
Requirements
To complete this tutorial you will need to install the following software and files:
Macromedia Flex
(Installed and running on any server and platform supported by Flex. Note that the Flex trial is available on CD from the Macromedia store for $8.99. To learn more about the Flex trial, please read the Flex Frequently Asked Questions.)
Dreamweaver MX 2004 or a text editor that you can use to write XML and ActionScript code (a basic text editor such as Notepad is adequate)
Solution files for the code
data_model.zip
(This ZIP file contains the source code for the following in this article:
data.mxml,
Employee.as,
DataController.as,
employees.xml,
locations.xml.)
Save the files in the flex_tutorials directory.
Prerequisite knowledge:
- Read A Brief Overview of Flex.
- You must know how to browse a Flex application.
- You should have completed
- Learning Flex Basics (Part 1): Creating Your First Flex Application
- Learning Flex Basics (Part 2): Creating a Flex Calculator
- Learning Flex Basics (Part 3): Working with Containers
Or you should have a basic understanding of how Flex applications work.
- If you have not completed the previous tutorials, create a directory call flex_tutorials under the Flex context root to save your code in.
Building the Application
The application view consists of three panels. One contains a DataGrid component that displays names and locations for all employees, and also provides a simple form for adding a new employee. The other two panels show DataGrids for the Engineering Group and Support Group. You implement drag and drop so that users can drag employees from the general employee grid and add them to either the engineering group or support group grid.
You use data models to import data for existing employees and office locations from XML files. A third model captures data from the employee entry form to add to the employee grid.
You create two associated ActionScript files. The DataController class contains methods to handle events in the application. The controller uses a separate Employee class as a factory to generate new employee data objects.
Figure 1. The finished application